package cn.regexp.common.core.exception;

import cn.regexp.common.core.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Regexpei
 * @date 2024/7/3 22:17
 * @description 统一异常处理
 */
@Slf4j
public class CommonControllerAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public <T> Result<T> handleBusinessException(BusinessException e) {
        return Result.error(e.getCodeMsg());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public <T> Result<T> handleDefaultException(Exception e) {
        log.error("Error message: {}", e.getMessage(), e);
        return Result.defaultError();
    }

}
